﻿function setRoomState(id, event, color) {
	var room = $('#o_' + id);
	var newid = id;
	if (color == null) {
		color = "black";
	}
	$('#' + id).remove();
	var offset = room.offset();
	$("<div/>").css({
		"position" : "absolute",
		"left" : offset.left,
		"top" : offset.top,
		"width" : room.width(),
		"height" : room.height(),
		"backgroundColor" : color,
		"opacity" : "0.5"
	}).attr('id', newid).click(event).appendTo('#__01');
}

function showRoom(room) {
	// 通过url地址获取层数的参数
	var url = parent.window.location.href;
	var floor = $.query.load(url).get("f");
	$('#currentRoom').html(room);
	$.post("AjaxAction!getBedInfo.action", {
		room_number : room,
		floor : floor
	}, function(data) {
		$('#bed').empty();
		if (data[0]) {
			showBed(data);
			$('#new_' + room).remove();
			// var obj = $('#' + room);
			var func = function() {
				showRoom(room);
			};
			if (data[0].chooseAble == 0 || data[0].total == data[0].current) {
				setRoomState(room, func, "black");
				/*
				 * var offset = obj.offset(); var color = "black"; $("<div/>").css({
				 * "position" : "absolute", "left" : offset.left, "top" :
				 * offset.top, "width" : obj.width(), "height" : obj.height(),
				 * "backgroundColor" : color, "opacity" : "0.5" }).attr('id',
				 * 'new_' + room).click(function() { showRoom(obj.attr("id"));
				 * }).appendTo('#__01');
				 */
			} else if (data[0].gender_type == '0') { // 男生房间用蓝色遮罩层
				setRoomState(room, func, "blue");
			} else { // 女生房间用红色遮罩层表示
				setRoomState(room, func, "red");
			}
			$('#roomState').html(data[0].chooseAble == 1 ? "开放" : "关闭");
			$('#roomSex').html(data[0].gender_type == 0 ? "男生" : "女生");
			$('#changeBt').attr("disabled", "");
		} else {
			if (confirm('抱歉，没有该房间信息，是否现在设置？')) {
				// 弹出对话框设置房间信息
				parent.window.setRoom(room);
			} else {
				$('#roomState').html('没有信息');
				$('#roomSex').html('没有信息');
				$('#changeBt').attr("disabled", "disabled");
			}
		}
		var bj = 0;
		$(data).each(function(i) {
			if (data[i].isChoosen == "0") {
				bj++;
			}
		});
		$('#leftBed').html(bj);
	}, "json");
}

function changeRoomState() {
	var room = $('#currentRoom').html();
	parent.setRoom(room, "modify");
}

function bedFunction(id, isSelected, gender) {
	if (isSelected == 1) {
		var room = id.substring(0, id.length - 3);
		var bed = id.substring(id.length - 1);
		// console.log(id+","+room+","+bed);
		$.post("AjaxAction!getChoosenInfoByAdmin.action", {
			room : room,
			bed : bed
		}, function(data) {
			if (data[0].result == "logout") {
				alert("对不起，登陆超时，请重新登录！");
				top.location.href = "login.html";
				return false;
			} else if (data[0].result == "error") {
				alert("错误！请与管理员联系");
			} else if (data[0].result == "choosed") {
				alert("学号为" + data[0].student + "的同学选择了该床位");
			} else if (data[0].result == "unchoosed") {
				alert("该床位还没有被选择！");
				showRoom(room);
			} else if (data[0].result == "permission denied") {
				alert("对不起，您没有权限查看该信息！");
			} else {
				alert("错误，可能是网络不通，请稍后重试，如果问题一直存在，请与管理员联系！");
			}
		}, "json");
	} else {
		alert("该床位还没有被选择！");
	}
}

/*
 * what is going to be done is just how to use the information successfully when
 * we choose the room we have to see the current status of the room,and we must
 * make it useful that we make the room
 */
function showBed(data) {
	var type = data[0].type;
	var thisroom = document.getElementById("bed");
	var gender = data[0].gender_type;
	if (type == 4) {
		var isChoosen = data[0].isChoosen;
		var bed1 = document.createElement("DIV");
		bed1.id = data[0].room_number + "_b1";
		bed1.style.position = "absolute";
		bed1.style.left = '1px';
		bed1.style.top = '62px';
		if (isChoosen == 1)
			bed1.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (isChoosen == 0)
			bed1.style.backgroundImage = 'url(./images/bednormal.png)';
		bed1.style.width = "58px";
		bed1.style.height = "50px";
		bed1.setAttribute("onclick", "bedFunction('" + bed1.id + "',"
				+ isChoosen + "," + gender + ")");
		thisroom.appendChild(bed1);
		var bed2 = document.createElement("DIV");
		isChoosen = data[1].isChoosen;
		bed2.id = data[0].room_number + "_b2";
		bed2.style.position = "absolute";
		bed2.style.left = "61px";
		bed2.style.top = "62px";
		if (isChoosen == 1)
			bed2.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (isChoosen == 0)
			bed2.style.backgroundImage = 'url(./images/bednormal.png)';
		bed2.style.width = "58px";
		bed2.style.height = "50px";
		bed2.setAttribute("onclick", "bedFunction('" + bed2.id + "',"
				+ isChoosen + "," + gender + ")");
		thisroom.appendChild(bed2);
		var bed3 = document.createElement("DIV");
		isChoosen = data[2].isChoosen;
		bed3.id = data[0].room_number + "_b3";
		bed3.style.position = "absolute";
		bed3.style.left = "128px";
		bed3.style.top = "62px";
		if (isChoosen == 1)
			bed3.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (isChoosen == 0)
			bed3.style.backgroundImage = 'url(./images/bednormal.png)';
		bed3.style.width = "58px";
		bed3.style.height = "50px";
		bed3.setAttribute("onclick", "bedFunction('" + bed3.id + "',"
				+ isChoosen + "," + gender + ")");
		thisroom.appendChild(bed3);
		var bed4 = document.createElement("DIV");
		isChoosen = data[3].isChoosen;
		bed4.id = data[0].room_number + "_b4";
		bed4.style.position = "absolute";
		bed4.style.left = "189px";
		bed4.style.top = "62px";
		if (isChoosen == 1)
			bed4.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (isChoosen == 0)
			bed4.style.backgroundImage = 'url(./images/bednormal.png)';
		bed4.style.width = "58px";
		bed4.style.height = "50px";
		bed4.setAttribute("onclick", "bedFunction('" + bed4.id + "',"
				+ isChoosen + "," + gender + ")");
		thisroom.appendChild(bed4);
	} else if (type == 2) {
		var bed1 = document.createElement("DIV");
		var isChoosen = data[0].isChoosen;
		bed1.id = data[0].room_number + "_b1";
		bed1.style.position = "absolute";
		bed1.style.left = '47px';
		bed1.style.top = '60px';
		if (isChoosen == 1)
			bed1.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (isChoosen == 0)
			bed1.style.backgroundImage = 'url(./images/bednormal.png)';
		bed1.style.width = "58px";
		bed1.style.height = "50px";
		bed1.setAttribute("onclick", "bedFunction('" + bed1.id + "',"
				+ isChoosen + "," + gender + ")");
		thisroom.appendChild(bed1);
		var bed2 = document.createElement("DIV");
		isChoosen = data[1].isChoosen;
		bed2.id = data[0].room_number + "_b2";
		bed2.style.position = "absolute";
		bed2.style.left = "145px";
		bed2.style.top = "60px";
		if (isChoosen == 1)
			bed2.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (isChoosen == 0)
			bed2.style.backgroundImage = 'url(./images/bednormal.png)';
		bed2.style.width = "58px";
		bed2.style.height = "50px";
		bed2.setAttribute("onclick", "bedFunction('" + bed2.id + "',"
				+ isChoosen + "," + gender + ")");
		thisroom.appendChild(bed2);
	} else if (type == 3) {
		var bed1 = document.createElement("DIV");
		bed1.id = data[0].room_number + "_b1";
		bed1.style.position = "absolute";
		bed1.style.left = '5px';
		bed1.style.top = '59px';
		if (data[0].isChoosen == 1)
			bed1.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (data[0].isChoosen == 0)
			bed1.style.backgroundImage = 'url(./images/bednormal.png)';
		bed1.style.width = "58px";
		bed1.style.height = "50px";
		bed1.setAttribute("onclick", "bedFunction('" + bed1.id + "',"
				+ data[0].isChoosen + "," + gender + ")");
		thisroom.appendChild(bed1);
		var bed2 = document.createElement("DIV");
		bed2.id = data[0].room_number + "_b2";
		bed2.style.position = "absolute";
		bed2.style.left = "180px";
		bed2.style.top = "59px";
		if (data[1].isChoosen == 1)
			bed2.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (data[1].isChoosen == 0)
			bed2.style.backgroundImage = 'url(./images/bednormal.png)';
		bed2.style.width = "58px";
		bed2.style.height = "50px";
		bed2.setAttribute("onclick", "bedFunction('" + bed2.id + "',"
				+ data[1].isChoosen + "," + gender + ")");
		thisroom.appendChild(bed2);
		var bed3 = document.createElement("DIV");
		bed3.id = data[0].room_number + "_b3";
		bed3.style.position = "absolute";
		bed3.style.left = "5px";
		bed3.style.top = "155px";
		if (data[2].isChoosen == 1)
			bed3.style.backgroundImage = 'url(./images/bedselect.png)';
		else if (data[2].isChoosen == 0)
			bed3.style.backgroundImage = 'url(./images/bednormal.png)';
		bed3.style.width = "58px";
		bed3.style.height = "50px";
		bed3.setAttribute("onclick", "bedFunction('" + bed3.id + "',"
				+ data[2].isChoosen + "," + gender + ")");
		thisroom.appendChild(bed3);
	}else if(type==1){
		var id=data[0].room_number+"_b1";
		var db1=$("<div/>").css({
			"left":'5px',
			"top":'59px',
			"width":'58px',
			"height":'50px',
			"position":'absolute'
		}).attr("id", id).click(function(){
			bedFunction(id, data[0].isChoosen, gender);
		}).appendTo('#bed');
		if(data[0].isChoosen==1){
			db1.css('background-image', 'url(./images/bedselect.png)');
		}else if(data[0].isChoosen == 0){
			db1.css('background-image', 'url(./images/bednormal.png)');
		}
	}
	/*
	 * if(type == 1) bed.style.backgroundImage="url(./images/layout1.png)"; else
	 * if(type == 2) bed.style.backgroundImage="url(./images/layout2.png)"; else
	 * if(type == 3) bed.style.backgroundImage="url(./images/layout3.png)";
	 */
	thisroom.style.backgroundImage = 'url(./images/layout3back.jpg)';
	thisroom.style.visibility = "visible";
}

function full() {
	window.alert("您好，这个个房间已经选满了，请选择其他房间!");
}
function closed() {
	window.alert("抱歉，该房间已经被关闭，您无法选择!");
}